package com.aiyige.share;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.api.ApiManager;
import com.aiyige.im.SessionHelper;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.share.model.ShareEntity;
import com.aiyige.share.model.ShareLogRequest;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.widget.RoundCornerImageView;
import com.netease.nim.uikit.common.util.string.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ShareToContactDialog extends DialogFragment {
    Unbinder unbinder;
    @BindView(R.id.contactAvatarIv)
    ImageView contactAvatarIv;
    @BindView(R.id.contactNameTv)
    TextView contactNameTv;
    @BindView(R.id.userAvatarIv)
    RoundCornerImageView userAvatarIv;
    @BindView(R.id.userNameTv)
    TextView userNameTv;
    @BindView(R.id.userIntroductionTv)
    TextView userIntroductionTv;
    @BindView(R.id.shareUserLayout)
    View shareUserLayout;
    @BindView(R.id.momentCoverIv)
    RoundCornerImageView momentCoverIv;
    @BindView(R.id.momentDetailTv)
    TextView momentDetailTv;
    @BindView(R.id.shareMomentLayout)
    View shareMomentLayout;
    @BindView(R.id.cancelBtn)
    TextView cancelBtn;
    @BindView(R.id.sendBtn)
    TextView sendBtn;
    @BindView(R.id.playIv)
    ImageView playIv;
    ShareEntity shareEntity;

    public ShareToContactDialog() {
    }

    public ShareEntity getShareEntity() {
        return shareEntity;
    }

    public void setShareEntity(ShareEntity shareEntity) {
        this.shareEntity = shareEntity;
    }

    public static ShareToContactDialog newInstance(ShareEntity shareEntity) {
        ShareToContactDialog fragment = new ShareToContactDialog();
        fragment.setStyle(STYLE_NORMAL, R.style.CommonDialogTheme);
        fragment.setShareEntity(shareEntity);
        fragment.setCancelable(false);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.share_to_contact_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        contactNameTv.setText(shareEntity.getContact().getName());
        GlideUtil.with(view.getContext())
                .loadAvatar(shareEntity.getContact().getAvatar())
                .into(contactAvatarIv);
        switch (shareEntity.getShareType()) {
            case ShareEntity.SHARE_TYPE_LINK: {
                shareUserLayout.setVisibility(View.INVISIBLE);
                shareMomentLayout.setVisibility(View.VISIBLE);
                momentCoverIv.setImageResource(R.mipmap.ic_launcher);
                momentDetailTv.setText(shareEntity.getLink());
                playIv.setVisibility(View.INVISIBLE);
                break;
            }
            case ShareEntity.SHARE_TYPE_MOMENT: {
                shareUserLayout.setVisibility(View.INVISIBLE);
                shareMomentLayout.setVisibility(View.VISIBLE);
                GlideUtil.with(view.getContext())
                        .loadCover(StringUtils.escapeEmptyString(ListUtil.getFirstItem(shareEntity.getMoment().getCover())))
                        .into(momentCoverIv);
                momentDetailTv.setText(StringUtils.escapeEmptyString(shareEntity.getMoment().getTitle()));
                switch (shareEntity.getMoment().getSubject()) {
                    case Moment.SUBJECT_SPECIAL_TOPIC:
                    case Moment.SUBJECT_ARTICLE:
                    case Moment.SUBJECT_MAJOR_COURSE:
                    case Moment.SUBJECT_PHOTO:
                        playIv.setVisibility(View.INVISIBLE);
                        break;
                    case Moment.SUBJECT_VIDEO:
                    case Moment.SUBJECT_VIDEO_COURSE:
                        playIv.setVisibility(View.VISIBLE);
                        break;
                    default:
                        playIv.setVisibility(View.INVISIBLE);
                        break;
                }
                break;
            }
            case ShareEntity.SHARE_TYPE_USER: {
                shareUserLayout.setVisibility(View.VISIBLE);
                shareMomentLayout.setVisibility(View.INVISIBLE);
                GlideUtil.with(view.getContext())
                        .loadCover(StringUtils.escapeEmptyString(shareEntity.getUser().getAvatar()))
                        .into(userAvatarIv);
                userNameTv.setText(StringUtils.escapeEmptyString(shareEntity.getUser().getAccount()));
                userIntroductionTv.setText(StringUtils.escapeEmptyString(shareEntity.getUser().getMoreBackup().getIntroduce()));
                break;
            }
        }


        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.cancelBtn, R.id.sendBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancelBtn:
                dismiss();
                break;
            case R.id.sendBtn:
                try {
                    ShareLogRequest shareLogRequest = ShareLogRequest.newBuilder()
                            .createTime(System.currentTimeMillis())
                            .destination(ShareLogRequest.DESTINATION_EIG_APP)
                            .build();
                    switch (shareEntity.getShareType()) {
                        case ShareEntity.SHARE_TYPE_LINK:
                            shareLogRequest.setObjectType(ShareLogRequest.OBJECT_TYPE_LINK);
                            shareLogRequest.setObjectId("");
                            break;
                        case ShareEntity.SHARE_TYPE_MOMENT:
                            shareLogRequest.setObjectType(ShareLogRequest.OBJECT_TYPE_MOMENT);
                            shareLogRequest.setObjectId(shareEntity.getMoment().getId());
                            break;
                        case ShareEntity.SHARE_TYPE_USER:
                            shareLogRequest.setObjectType(ShareLogRequest.OBJECT_TYPE_USER);
                            shareLogRequest.setObjectId(shareEntity.getUser().getId());
                            break;
                    }
                    ApiManager.getService().shareLog(shareLogRequest).enqueue(ApiManager.NOOP_CALLBACK);
                } catch (Exception e) {

                }
                switch (shareEntity.getShareType()) {
                    case ShareEntity.SHARE_TYPE_MOMENT: {
                        try {
                            String countStr = "";
                            switch (shareEntity.getMoment().getSubject()) {
                                case Moment.SUBJECT_MAJOR_COURSE: {
                                    String str = StringUtils.formatNum(shareEntity.getMoment().getInventoryBackup().getAvailableInventory());
                                    countStr = StringUtils.getString(R.string.left_quota_of_people, str);
                                    break;
                                }
                                case Moment.SUBJECT_VIDEO_COURSE: {
                                    String str = StringUtils.formatNum(shareEntity.getMoment().getStatisticsBackup().getSellCount());
                                    countStr = StringUtils.getString(R.string.goods_sell_number, str);
                                    break;
                                }
                                default: {
                                    String str = StringUtils.formatNum(shareEntity.getMoment().getStatisticsBackup().getViewCount());
                                    countStr = StringUtils.getString(R.string.moment_view_count, str);
                                    break;
                                }
                            }
                            String price;
                            String linePrice;
                            if (shareEntity.getMoment().getOnSalesBackup().hasLinePrice()) {
                                price = StringUtils.priceFormat(shareEntity.getMoment().getOnSalesBackup().getPrice());
                                linePrice = StringUtils.priceFormat(shareEntity.getMoment().getCommodityBackup().getPrice());
                            } else {
                                price = StringUtils.priceFormat(shareEntity.getMoment().getCommodityBackup().getPrice());
                                linePrice = "";
                            }


                            SessionHelper.getInstance()
                                    .sendMomentMessage(StringUtils.escapeEmptyString(shareEntity.getContact().getAccid()),
                                            StringUtils.escapeEmptyString(shareEntity.getMoment().getSubject()),
                                            StringUtils.escapeEmptyString(ListUtil.getFirstItem(shareEntity.getMoment().getCover())),
                                            StringUtils.escapeEmptyString(shareEntity.getMoment().getTitle()),
                                            countStr,
                                            StringUtils.escapeEmptyString(shareEntity.getMoment().getRouter()),
                                            price,
                                            linePrice);
                            ToastX.show(R.string.share_to_contact_success);
                        } catch (Exception e) {

                        }
                        break;
                    }
                    case ShareEntity.SHARE_TYPE_LINK: {
                        try {
                            String countStr = "";
                            SessionHelper.getInstance()
                                    .sendMomentMessage(StringUtils.escapeEmptyString(shareEntity.getContact().getAccid()),
                                            Moment.SUBJECT_ARTICLE,
                                            "",
                                            StringUtils.escapeEmptyString(shareEntity.getLink()),
                                            countStr,
                                            StringUtils.escapeEmptyString(shareEntity.getLink()),
                                            "", "");
                            ToastX.show(R.string.share_to_contact_success);
                        } catch (Exception e) {

                        }
                        break;
                    }


                    case ShareEntity.SHARE_TYPE_USER: {
                        try {
                            String countStr = "";
                            try {
                                String str = StringUtils.formatNum(shareEntity.getUser().getStatisticsBackup().getFollowersCount());
                                countStr = StringUtils.getString(R.string.user_follow_count, str);
                            } catch (Exception e) {

                            }
                            SessionHelper.getInstance()
                                    .sendUserHomePageMessage(StringUtils.escapeEmptyString(shareEntity.getContact().getAccid()),
                                            StringUtils.escapeEmptyString(shareEntity.getUser().getAvatar()),
                                            StringUtils.escapeEmptyString(shareEntity.getUser().getAccount()),
                                            countStr,
                                            StringUtils.escapeEmptyString(shareEntity.getUser().getRouter()));
                            ToastX.show(R.string.share_to_contact_success);
                        } catch (Exception e) {

                        }
                        break;
                    }
                }

                dismiss();
                break;
        }
    }
}
